package com.sunchen.asc.service.user.repository;

import com.sunchen.asc.service.user.domain.Role;
import com.sunchen.asc.service.user.vo.RoleVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface RoleRepository extends JpaRepository<Role,Long> {

    @Query( "select t.name from Role t where t.id in ?1 and t.deleteFlag = 0")
    List<String> findNameInIds(List<Long> ids);

    Long countByNameAndDeleteFlag(String name,int deleteFlag);

    @Query(value = "SELECT user_id from user_roles where roles_id = ?1",nativeQuery = true)
    List<Long> findUsers(Long id);

    Role findFirstByNameAndDeleteFlag(String name, int i);

    @Query("select t.name from Role t where t.id = ?1")
    String findNameById(Long id);

    Page<Role> findByNameLikeAndDeleteFlag(String name, int deleteFlag, Pageable pageable);

    @Query("select new com.sunchen.asc.service.user.vo.RoleVo(t.id,t.name, t.status, t.title, t.description) from Role t where t.status = ?1 and t.deleteFlag = ?2")
    List<RoleVo> findVoByStatusAndDeleteFlag(int status,int deleteFlag);

    @Query(value = "SELECT * from role WHERE id in(SELECT role_resources.role_id from role_resources WHERE resources_id = ?1)",nativeQuery = true)
    List<Role> getRoleByUrlId(Long id);
}
